   package com.longkier.gx_library_springboot.repository;

   import com.longkier.gx_library_springboot.entity.User;
   import org.apache.ibatis.annotations.Param;
   import org.springframework.data.jpa.repository.JpaRepository;
   import org.springframework.data.jpa.repository.Modifying;
   import org.springframework.data.jpa.repository.Query;
   import org.springframework.data.domain.Page;
   import org.springframework.data.domain.Pageable;
   import org.springframework.stereotype.Repository;
   import org.springframework.transaction.annotation.Transactional;

   import java.util.List;
   import java.util.Optional;

   @Transactional
   @Repository
   public interface UserRepository extends JpaRepository<User, Long> {
       Optional<User> findByUsername(String username);
       Optional<User> findByCardNumber(String cardNumber);
       boolean existsByCardNumber(String cardNumber);

       @Modifying
       @Query("DELETE FROM User u WHERE u.userId IN :userIds")
       int deleteAllByIdInBatch(@Param("userIds") List<Long> userIds);
       
       // 新增模糊查询方法
       @Query("SELECT u FROM User u " +
               "WHERE (:status IS NULL OR u.status = :status) " +
               "AND (:username IS NULL OR u.username LIKE %:username%) " +
               "AND (:cardName IS NULL OR u.cardName LIKE %:cardName%) " +
               "AND (:cardNumber IS NULL OR u.cardNumber LIKE %:cardNumber%)")
       Page<User> findByCriteria(@Param("status") Integer status,
                                 @Param("username") String username,
                                 @Param("cardName") String cardName,
                                 @Param("cardNumber") String cardNumber,
                                 Pageable pageable);

   }